package com.yundin.service.impl;

import com.yundin.constant.StatusConstant;
import com.yundin.entity.Orders;
import com.yundin.mapper.DishMapper;
import com.yundin.mapper.OrderMapper;
import com.yundin.mapper.SetmealMapper;
import com.yundin.mapper.UserMapper;
import com.yundin.service.WorkSpacService;
import com.yundin.vo.BusinessDataVO;
import com.yundin.vo.DishOverViewVO;
import com.yundin.vo.OrderOverViewVO;
import com.yundin.vo.SetmealOverViewVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class WorkSpacServiceImpl implements WorkSpacService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {
        Map map = new HashMap();
        map.put("begin",begin);
        map.put("end",end);

        //查询订单总数
        Integer totalOrderCount = orderMapper.countByMap(map);
        map.put("status", Orders.COMPLETED);
        //查询营业额
        Double turnover = orderMapper.sumByMap(map);
        turnover = turnover==null?0.0:turnover;
        //有效订单数
        Integer validOrderCount = orderMapper.countByMap(map);
        //客单价
        Double unitPrice =0.0;
        //有效订单率
        Double orderCompleteionRate=0.0;
        if(totalOrderCount!=0 && validOrderCount!=0){
            orderCompleteionRate = validOrderCount.doubleValue()/totalOrderCount;
            unitPrice = turnover / validOrderCount;
        }
        //新增用户数
        Integer newUsers = userMapper.getCountByMap(map);

        return BusinessDataVO.builder()
                .newUsers(newUsers)
                .unitPrice(unitPrice)
                .orderCompletionRate(orderCompleteionRate)
                .validOrderCount(validOrderCount)
                .turnover(turnover)
                .build();
    }

    @Override
    public OrderOverViewVO getOrderView() {
        Map map = new HashMap();
        map.put("begin",LocalDateTime.now().with(LocalTime.MIN));
        map.put("status", Orders.TO_BE_CONFIRMED);
        //待接单数据
        Integer waitingOrder = orderMapper.countByMap(map);
        map.put("status", Orders.CONFIRMED);
        //已经截单 等待派送订单
        Integer deliveredOrder = orderMapper.countByMap(map);
        map.put("status", Orders.COMPLETED);
        //已完成
        Integer completedOrder = orderMapper.countByMap(map);
        map.put("status", Orders.CANCELLED);
        Integer cancelledOrder = orderMapper.countByMap(map);
        map.put("status", null);
        Integer allOrder = orderMapper.countByMap(map);

        return OrderOverViewVO.builder()
                .allOrders(allOrder)
                .cancelledOrders(cancelledOrder)
                .completedOrders(completedOrder)
                .deliveredOrders(deliveredOrder)
                .waitingOrders(waitingOrder).build();
    }

    @Override
    public DishOverViewVO getDishOverView() {
        Map map = new HashMap();
        map.put("status", StatusConstant.ENABLE);
        Integer sold = dishMapper.countByMap(map);

        map.put("status", StatusConstant.DISABLE);
        Integer discontiuned = dishMapper.countByMap(map);

        return DishOverViewVO.builder()
                .sold(sold)
                .discontinued(discontiuned).build();
    }

    @Override
    public SetmealOverViewVO getSetmealOverView() {
        Map map = new HashMap();
        map.put("status", StatusConstant.ENABLE);
        Integer sold = setmealMapper.countByMap(map);

        map.put("status", StatusConstant.DISABLE);
        Integer discontiuned = setmealMapper.countByMap(map);

        return SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(discontiuned).build();
    }
}
